package com.dys.datastructuresandalgorithm.factorial;

/**
 * @className: com.dys.datastructuresandalgorithm.factorial.FactorialCode
 * @description: 阶乘练习：给定一个参数N,返回1!+2!+3!+...+N！的结果
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/05/16 下午11:15
 */
public class FactorialCode {

    /**
     * 方法1：1!+2!(1*2)+3!(1*2*3)+4!(1*2*3*4)+N!
     *
     * @param N N的阶乘
     * @return 阶乘的结果
     */
    public static long f1(int N) {
        long ans = 0;
        for (int i = 1; i <= N; i++) {
            ans += factorial(i);
        }
        return ans;
    }

    private static long factorial(int N) {
        long ans = 1;
        for (int j = 1; j <= N; j++) {
            ans *= j;
        }
        return ans;
    }

    /**
     * 方法2：
     * 1!=1
     * 2!=1!*2
     * 3!=2!*3
     * 4!=3!*4
     * N!=
     *
     * @param N N的阶乘
     * @return 阶乘的结果
     */
    private static long f2(int N) {
        long ans = 0;
        long cur = 1;
        for (int i = 1; i <= N; i++) {
            cur = cur * i;
            ans += cur;
        }
        return ans;
    }

    public static void main(String[] args) {
        int N = 10;
        System.out.println(f1(N));
        System.out.println(f2(N));
    }
}
